<template>
  <el-dialog
    :title="title"
    :visible.sync="openSheet"
    width="50%"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    :before-close="cancel"
  >
    <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('serialNumber')" prop="serial_number" :error="error.serial_number">
            <el-input v-model="form.serial_number" maxlength="10" show-word-limit :disabled="!createFlag" :placeholder="$t('inputserialNumber')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('serialNumberName')" prop="serial_name" :error="error.serial_name">
            <el-input v-model="form.serial_name" maxlength="30" show-word-limit :placeholder="$t('inputserialNumberName')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="createFlag" type="primary" :disabled="!isModified" @click="handleCreate">{{ $t('create') }}</el-button>
      <el-button v-else type="primary" :disabled="!isModified" @click="handleUpdate">{{ $t('update') }}</el-button>
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import sheetMixin from '@/mixins/SheetMixin'
import { createSerialNumApi, updateSerialNumApi } from '../api/serialnumber.api';
import { getDropDownMenuListApi } from '@/api/common/common'

export default {
  mixins: [sheetMixin],
  data() {
    return {
      formRef: 'form',
      createTitle: this.$t('serialNumberCreateTitle'),
      updateTitle: this.$t('serialNumberUpdateTitle'),
      menuOptions: [],
      actionApi: {
        'create': createSerialNumApi,
        'update': updateSerialNumApi
      },
      form: {
        serial_number: undefined,
        serial_name: undefined
      },
      error: {
        serial_number: undefined,
        serial_name: undefined
      },
      rules: {
        serial_number: [{ required: true, message: this.$t('serialNumberRequired'), trigger: 'blur' }]
      }
    };
  },
  computed: {
    labelWidth() {
      return this.$i18n.locale === 'en' ? '150px' : '80px';
    }
  },
  methods: {
    initialize() {
      this.getMenuOption()
    },
    async getMenuOption() {
      var resp = await getDropDownMenuListApi();
      if (resp.success) {
        this.menuOptions = resp.data;
      } else {
        this.targaMessageBox(this, resp.msg);
      }
    },
    handleCreate() {
      this.submitForm('create', this.$t('createSuccess', { title: this.$t('serialNumber'), value: this.form.serial_number }))
    },
    handleUpdate() {
      this.submitForm('update', this.$t('updateSuccess', { title: this.$t('serialNumber'), value: this.form.serial_number }))
    }
  }
};
</script>

  <style scoped>
  </style>
